﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Framework;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Web.UI.WebControls;

    public class CartList : UserControlBase
    {
        private bool _AllowPriceEdit;
        private string _CartID;
        [AccessedThroughProperty("cmdRecalculate")]
        private LinkButton _cmdRecalculate;
        [AccessedThroughProperty("dgCartList")]
        private DataGrid _dgCartList;
        private Hashtable _HideColumns;
        private bool _HideHeader;
        private bool _HideTotals;
        [AccessedThroughProperty("lblEstShipDate")]
        private Label _lblEstShipDate;
        [AccessedThroughProperty("lblMsg")]
        private Label _lblMsg;
        [AccessedThroughProperty("lblTax")]
        private Label _lblTax;
        [AccessedThroughProperty("lblTaxAmount")]
        private Label _lblTaxAmount;
        private bool _NoUpdates;
        private int _PortalID;
        private string _ResourceFile;
        private int _ShipMethodID;
        private bool _ShippingHidden;
        private bool _ShowDiscountCol;
        private string _TaxOption;
        private decimal cartTotal;
        private int itemCount;

        public event CartIsEmptyEventHandler CartIsEmpty;

        public event RecalculateCartEventHandler RecalculateCart;

        public event ValidateCartEventHandler ValidateCart;

        public CartList()
        {
            base.Load += new EventHandler(this.Page_Load);
            this._CartID = "";
            this._NoUpdates = true;
            this.cartTotal = new decimal();
            this.itemCount = 0;
            this._TaxOption = "1";
            this._ShipMethodID = -1;
            this._ShippingHidden = false;
            this._ShowDiscountCol = false;
            this._HideColumns = new Hashtable();
            this._HideTotals = false;
            this._AllowPriceEdit = false;
            this._HideHeader = false;
        }

        private ArrayList AddLinktoDescription(ArrayList aryList, bool AddThumb)
        {
            IEnumerator enumerator=null;
            string str = "";
            try
            {
                enumerator = aryList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    NB_Store_CartListInfo current = (NB_Store_CartListInfo) enumerator.Current;
                    str = "";
                    if (AddThumb)
                    {
                        ProductController controller = new ProductController();
                        NB_Store_ModelInfo model = controller.GetModel(current.ModelID, SharedFunctions.GetCurrentCulture());
                        if (model != null)
                        {
                            ArrayList productImageList = controller.GetProductImageList(model.ProductID, SharedFunctions.GetCurrentCulture());
                            if (productImageList.Count > 0)
                            {
                                NB_Store_ProductImageInfo info3 = (NB_Store_ProductImageInfo) productImageList[0];
                                if (info3 != null)
                                {
                                    int num = SharedFunctions.GetStoreSettingInt(Conversions.ToInteger(this.PortalID), "cartthumb.size", "NONE");
                                    if (num < 40)
                                    {
                                        num = 50;
                                    }
                                    str = str + "<img class='cartthumb' src='/DesktopModules/NB_Store/makethumbnail.ashx?Image=" + Conversions.ToString(info3.ImageID) + "&amp;w=" + Conversions.ToString(num) + "&amp;h=0' border='0' />";
                                }
                            }
                        }
                    }
                    str = str + "<a href='" + current.ProductURL + "' >" + current.ItemDesc + "</a>";
                    current.ItemDesc = str;
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            return aryList;
        }

        private void assignAfterDiscount(Label lblValue, Label lblLabel, CartTotals Totals)
        {
            if (decimal.Compare(Totals.DiscountAmt, decimal.Zero) == 0)
            {
                lblValue.Visible = false;
                lblLabel.Visible = false;
            }
            else
            {
                lblValue.Visible = true;
                lblLabel.Visible = true;
                if (decimal.Compare(decimal.Add(Totals.TotalAmt, Totals.DiscountAmt), decimal.Zero) < 0)
                {
                    lblValue.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), 0.0);
                }
                else
                {
                    lblValue.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(decimal.Add(Totals.TotalAmt, Totals.DiscountAmt)));
                }
            }
        }

        private void assignDiscount(Label lblValue, Label lblLabel, CartTotals Totals)
        {
            if (decimal.Compare(Totals.DiscountAmt, decimal.Zero) == 0)
            {
                lblValue.Visible = false;
                lblLabel.Visible = false;
                this._ShowDiscountCol = false;
            }
            else
            {
                lblValue.Visible = true;
                lblLabel.Visible = true;
                lblValue.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(Totals.DiscountAmt));
            }
            this.dgCartList.Columns[4].Visible = this._ShowDiscountCol;
        }

        private void assignShipping(Label lblShippingTotal, Label lblShipping, CartTotals Totals)
        {
            lblShippingTotal.Visible = true;
            lblShipping.Visible = true;
            lblShippingTotal.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(Totals.ShipAmt));
        }

        private void assignTax(Label lblAppliedTaxAmount, Label lblAppliedTax, CartTotals Totals)
        {
            this.lblTaxAmount.Visible = false;
            this.lblTax.Visible = false;
            lblAppliedTaxAmount.Visible = false;
            lblAppliedTax.Visible = false;
            string str = this._TaxOption;
            if (str != "1")
            {
                if (str == "2")
                {
                    this.lblTaxAmount.Visible = true;
                    this.lblTax.Visible = true;
                    this.lblTaxAmount.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(Totals.TaxAmt));
                }
                else if (str == "3")
                {
                    lblAppliedTaxAmount.Visible = true;
                    lblAppliedTax.Visible = true;
                    lblAppliedTaxAmount.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(Totals.TaxAppliedAmt));
                }
            }
        }

        private void cmdRecalculate_Click(object sender, EventArgs e)
        {
            this.UpdateDataGridQty();
            
            if (this.RecalculateCart != null)
            {
                RecalculateCart();
            }
        }

        private void dgCartList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            NB_Store_CartListInfo dataItem = (NB_Store_CartListInfo) e.Item.DataItem;
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Label label = (Label) e.Item.FindControl("lblUnitCost");
                if (label != null)
                {
                    label.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(dataItem.UnitCost));
                }
                Label label2 = (Label) e.Item.FindControl("lblDiscount");
                if (label2 != null)
                {
                    label2.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(decimal.Multiply(dataItem.Discount, new decimal(dataItem.Quantity))));
                }
                label = (Label) e.Item.FindControl("lblSubTotal");
                if (label != null)
                {
                    label.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(dataItem.SubTotal));
                    this.cartTotal = decimal.Add(this.cartTotal, dataItem.SubTotal);
                    this.itemCount += dataItem.Quantity;
                }
                if (this._NoUpdates)
                {
                    TextBox box2 = (TextBox) e.Item.FindControl("txtQty");
                    box2.Enabled = false;
                    CheckBox box = (CheckBox) e.Item.FindControl("chkRemove");
                    box.Enabled = false;
                    TextBox box3 = (TextBox) e.Item.FindControl("txtUnitCost");
                    box3.Enabled = false;
                }
                if (this._AllowPriceEdit)
                {
                    TextBox box4 = (TextBox) e.Item.FindControl("txtUnitCost");
                    box4.Visible = true;
                }
            }
            if ((e.Item.ItemType == ListItemType.Footer) & !this._HideTotals)
            {
                Label label3 = (Label) e.Item.FindControl("lblQtyCount");
                if (label3 != null)
                {
                    label3.Text = this.itemCount.ToString();
                }
                Label label5 = (Label) e.Item.FindControl("lblTotal");
                if (label5 != null)
                {
                    label5.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(this.cartTotal));
                }
                CartTotals calulatedTotals = CurrentCart.GetCalulatedTotals(Conversions.ToInteger(this.PortalID), this._ShipMethodID);
                Label lblValue = (Label) e.Item.FindControl("lblOrdTotal");
                if (lblValue != null)
                {
                    lblValue.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(calulatedTotals.OrderTotal));
                }
                lblValue = (Label) e.Item.FindControl("lblDiscountTotal");
                Label lblLabel = (Label) e.Item.FindControl("lblDiscount");
                if ((lblLabel != null) & (lblValue != null))
                {
                    this.assignDiscount(lblValue, lblLabel, calulatedTotals);
                }
                lblValue = (Label) e.Item.FindControl("lblTotalAfterDiscount");
                lblLabel = (Label) e.Item.FindControl("lblAfterDiscount");
                if ((lblLabel != null) & (lblValue != null))
                {
                    this.assignAfterDiscount(lblValue, lblLabel, calulatedTotals);
                }
                lblValue = (Label) e.Item.FindControl("lblShippingTotal");
                lblLabel = (Label) e.Item.FindControl("lblShipping");
                if ((lblLabel != null) & (lblValue != null))
                {
                    if (this.ShippingHidden)
                    {
                        lblValue.Visible = false;
                        lblLabel.Visible = false;
                    }
                    else
                    {
                        this.assignShipping(lblValue, lblLabel, calulatedTotals);
                    }
                }
                lblValue = (Label) e.Item.FindControl("lblAppliedTaxAmount");
                lblLabel = (Label) e.Item.FindControl("lblAppliedTax");
                if ((lblLabel != null) & (lblValue != null))
                {
                    this.assignTax(lblValue, lblLabel, calulatedTotals);
                }
                lblValue = (Label) e.Item.FindControl("lblOutstandingAmount");
                lblLabel = (Label) e.Item.FindControl("lblOutstanding");
                if ((lblLabel != null) & (lblValue != null))
                {
                    if (decimal.Compare(calulatedTotals.Balance, decimal.Zero) <= 0)
                    {
                        lblValue.Visible = false;
                        lblLabel.Visible = false;
                    }
                    else
                    {
                        lblValue.Text = SharedFunctions.FormatToStoreCurrency(Conversions.ToInteger(this.PortalID), Convert.ToDouble(calulatedTotals.Balance));
                    }
                }
            }
            if ((e.Item.ItemType == ListItemType.Footer) & this._HideTotals)
            {
                e.Item.Visible = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string str = "1";
            if (this.Request.QueryString["stg"] != null)
            {
                str = this.Request.QueryString["stg"];
            }
            if (!this.Page.IsPostBack & (str != "4"))
            {
                if (this._NoUpdates)
                {
                    this.cmdRecalculate.Visible = false;
                }
                CartTotals calulatedTotals = CurrentCart.GetCalulatedTotals(Conversions.ToInteger(this.PortalID), -1);
                if ((calulatedTotals != null) && (decimal.Compare(calulatedTotals.OrderTotal, decimal.Zero) == 0))
                {
                    this._HideTotals = true;
                    this.HideColumn = 2;
                    this.HideColumn = 6;
                    this.HideColumn = 4;
                }
                if (!this.Page.IsPostBack)
                {
                    if (this._ShipMethodID < 0)
                    {
                        this._ShipMethodID = SharedFunctions.GetDefaultShipMethod(Conversions.ToInteger(this.PortalID));
                    }
                    this.PopulateCartList();
                }
                IDictionaryEnumerator enumerator = this._HideColumns.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    DictionaryEntry current = (DictionaryEntry) enumerator.Current;
                    if (Operators.ConditionalCompareObjectLess(current.Value, this.dgCartList.Columns.Count, false))
                    {
                        this.dgCartList.Columns[Conversions.ToInteger(current.Value)].Visible = false;
                    }
                }
                if (this._HideHeader)
                {
                    this.dgCartList.ShowHeader = false;
                }
                if (this._HideTotals)
                {
                    this.lblTaxAmount.Visible = false;
                    this.lblTax.Visible = false;
                }
            }
        }

        public void PopulateCartList()
        {
            this.PopulateCartList(-1);
        }

        public void PopulateCartList(int OrderID)
        {
            CartController controller = new CartController();
            DataGrid dgCartList = this.dgCartList;
            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref dgCartList, this._ResourceFile);
            this.dgCartList = dgCartList;
            
            if (this.ValidateCart != null)
            {
                this.ValidateCart();
            }
            ArrayList cartList = controller.GetCartList(this._CartID);
            this.cartTotal = new decimal();
            this.itemCount = 0;
            this.dgCartList.Columns[4].Visible = this._ShowDiscountCol;
            if (cartList != null)
            {
                this.AddLinktoDescription(cartList, SharedFunctions.GetStoreSettingBoolean(Conversions.ToInteger(this.PortalID), "cartthumbs.flag", "NONE"));
                this.dgCartList.DataSource = cartList;
                this.dgCartList.DataBind();
            }
            if (this._NoUpdates)
            {
                OrderController controller2 = new OrderController();
                this.lblEstShipDate.Visible = false;
                NB_Store_CartInfo cart = controller.GetCart(this._CartID);
                if (cart != null)
                {
                    NB_Store_OrdersInfo order = controller2.GetOrder(cart.OrderID);
                    if ((order != null) && (DateTime.Compare(order.ShipDate, DateAndTime.Today) > 0))
                    {
                        this.lblEstShipDate.Text = Strings.Replace(DotNetNuke.Services.Localization.Localization.GetString("lblEstShipDate", this._ResourceFile), "[TAG:ESTIMATEDDATE]", DotNetNuke.Common.Globals.GetMediumDate(Conversions.ToString(order.ShipDate)), 1, -1, CompareMethod.Binary);
                        this.lblEstShipDate.Visible = true;
                    }
                }
            }
        }

        private void UpdateDataGridQty()
        {
            IEnumerator enumerator=null;
            CartController controller = new CartController();
            string str = "";
            int newQty = 0;
            SettingsController controller2 = new SettingsController();
            try
            {
                enumerator = this.dgCartList.Items.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    DataGridItem current = (DataGridItem) enumerator.Current;
                    TextBox box2 = (TextBox) current.FindControl("txtQty");
                    if (box2 != null)
                    {
                        CheckBox box = (CheckBox) current.FindControl("chkRemove");
                        if ((box != null) && box.Checked)
                        {
                            box2.Text = "0";
                        }
                        if (Versioned.IsNumeric(box2.Text) & Versioned.IsNumeric(current.Cells[0].Text))
                        {
                            if (Conversions.ToInteger(box2.Text) > 0)
                            {
                                newQty = CurrentCart.CheckStockByCartItemID(Conversions.ToInteger(this.PortalID), Conversions.ToInteger(current.Cells[0].Text), Conversions.ToInteger(box2.Text));
                                int num3 = 0xf423f;
                                NB_Store_SettingsInfo info = controller2.GetSetting(Conversions.ToInteger(this.PortalID), "productqty.limit", SharedFunctions.GetCurrentCulture());
                                if ((info != null) && Versioned.IsNumeric(info.SettingValue))
                                {
                                    num3 = Conversions.ToInteger(info.SettingValue);
                                    if (newQty > num3)
                                    {
                                        newQty = num3;
                                    }
                                }
                                if (newQty < Conversions.ToInteger(box2.Text))
                                {
                                    str = DotNetNuke.Services.Localization.Localization.GetString("QtyAdjusted", this._ResourceFile);
                                }
                                CurrentCart.UpdateCartItemQty(Conversions.ToInteger(current.Cells[0].Text), newQty);
                                decimal minusOne = decimal.MinusOne;
                                if (this._AllowPriceEdit)
                                {
                                    TextBox box3 = (TextBox) current.FindControl("txtUnitCost");
                                    if (Versioned.IsNumeric(box3.Text) && (decimal.Compare(Conversions.ToDecimal(box3.Text), decimal.Zero) >= 0))
                                    {
                                        minusOne = Conversions.ToDecimal(box3.Text);
                                    }
                                    if (decimal.Compare(minusOne, decimal.Zero) >= 0)
                                    {
                                        CurrentCart.UpdateCartItemUnitCost(Conversions.ToInteger(current.Cells[0].Text), minusOne);
                                    }
                                }
                            }
                            else
                            {
                                CurrentCart.RemoveItemFromCart(Conversions.ToInteger(current.Cells[0].Text));
                                if (CurrentCart.IsCartEmpty(Conversions.ToInteger(this.PortalID)))
                                {
                                    
                                    if (this.CartIsEmpty != null)
                                    {
                                        CartIsEmpty();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            this.lblMsg.Text = str;
            if (str != "")
            {
                this.lblMsg.Visible = true;
            }
            this.PopulateCartList();
        }

        public void UpdateQty()
        {
            this.UpdateDataGridQty();
        }

        public bool AllowPriceEdit
        {
            get
            {
                return this._AllowPriceEdit;
            }
            set
            {
                this._AllowPriceEdit = value;
            }
        }

        public string CartID
        {
            get
            {
                return this._CartID;
            }
            set
            {
                this._CartID = value;
            }
        }

        protected virtual LinkButton cmdRecalculate
        {
            get
            {
                return this._cmdRecalculate;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdRecalculate_Click);
                if (this._cmdRecalculate != null)
                {
                    this._cmdRecalculate.Click -= handler;
                }
                this._cmdRecalculate = value;
                if (this._cmdRecalculate != null)
                {
                    this._cmdRecalculate.Click += handler;
                }
            }
        }

        protected virtual DataGrid dgCartList
        {
            get
            {
                return this._dgCartList;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataGridItemEventHandler handler = new DataGridItemEventHandler(this.dgCartList_ItemDataBound);
                if (this._dgCartList != null)
                {
                    this._dgCartList.ItemDataBound -= handler;
                }
                this._dgCartList = value;
                if (this._dgCartList != null)
                {
                    this._dgCartList.ItemDataBound += handler;
                }
            }
        }

        public int HideColumn
        {
            set
            {
                if (!this._HideColumns.ContainsKey(value))
                {
                    this._HideColumns.Add(value, value);
                }
            }
        }

        public bool HideHeader
        {
            get
            {
                return this._HideHeader;
            }
            set
            {
                this._HideHeader = value;
            }
        }

        public bool HideTotals
        {
            get
            {
                return this._HideTotals;
            }
            set
            {
                this._HideTotals = value;
            }
        }

        protected virtual Label lblEstShipDate
        {
            get
            {
                return this._lblEstShipDate;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblEstShipDate = value;
            }
        }

        protected virtual Label lblMsg
        {
            get
            {
                return this._lblMsg;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblMsg = value;
            }
        }

        protected virtual Label lblTax
        {
            get
            {
                return this._lblTax;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblTax = value;
            }
        }

        protected virtual Label lblTaxAmount
        {
            get
            {
                return this._lblTaxAmount;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._lblTaxAmount = value;
            }
        }

        public bool NoUpdates
        {
            get
            {
                return this._NoUpdates;
            }
            set
            {
                this._NoUpdates = value;
            }
        }

        public string PortalID
        {
            get
            {
                return Conversions.ToString(this._PortalID);
            }
            set
            {
                this._PortalID = Conversions.ToInteger(value);
            }
        }

        public string ResourceFile
        {
            get
            {
                return this._ResourceFile;
            }
            set
            {
                this._ResourceFile = value;
            }
        }

        public string ShipMethodID
        {
            get
            {
                return Conversions.ToString(this._ShipMethodID);
            }
            set
            {
                this._ShipMethodID = Conversions.ToInteger(value);
            }
        }

        public bool ShippingHidden
        {
            get
            {
                return this._ShippingHidden;
            }
            set
            {
                this._ShippingHidden = value;
            }
        }

        public bool ShowDiscountCol
        {
            get
            {
                return this._ShowDiscountCol;
            }
            set
            {
                this._ShowDiscountCol = value;
            }
        }

        public string TaxOption
        {
            get
            {
                return this._TaxOption;
            }
            set
            {
                this._TaxOption = value;
            }
        }

        public delegate void CartIsEmptyEventHandler();

        public delegate void RecalculateCartEventHandler();

        public delegate void ValidateCartEventHandler();
    }
}

